package com.suncm3kw.chatgpt.config;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import com.suncm3kw.chatgpt.StaticValue;

/**
 * RestTemplate的配置
 * @Author 传明
 * @Date 2025年01月20日
 * @Version 1.0
 */
@Configuration
public class RestTemplateConfig {

	@ConditionalOnMissingBean(RestTemplate.class)
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate(getClientHttpRequestFactory());
    }

    /**
     * 使用HttpClient作为底层客户端
     */
	 private ClientHttpRequestFactory getClientHttpRequestFactory() {
		 RequestConfig config = RequestConfig.custom()
			.setConnectTimeout((int)StaticValue.SSE_TIMEOUT)
			.setConnectionRequestTimeout((int)StaticValue.SSE_TIMEOUT)
			.setSocketTimeout((int)StaticValue.SSE_TIMEOUT)
			.build();
		 CloseableHttpClient client = HttpClientBuilder
			.create()
			.setDefaultRequestConfig(config)
			.build();
	     return new HttpComponentsClientHttpRequestFactory(client);
	 }

}
